import { _decorator, Component, Node, Vec3 } from 'cc';
import { DiffSingleMajiang } from './DiffSingleMajiang';
import { DiffMajiang } from './DiffMajiang';
import { SOUND_TYPE } from '../../Data/GlobalData';
const { ccclass, property } = _decorator;

@ccclass('LittleMajiangBox')
export class LittleMajiangBox extends Component {

    //麻将层
    @property(Node)
    public Content: Node = null;

    //解锁按钮
    @property(Node)
    public UnLockBtn: Node = null;

    //显示麻将
    public ShowMajiang: Node[] = [];

    //点击得麻将
    private ClickMajiang: Node[] = [];

    public DiffMajiang: DiffMajiang = null;


    /**
     * 初始化
     */
    public Init(_name: string[], _isani: boolean = false) {

        this.Content.active = true;
        this.UnLockBtn.active = false;
        this.ShowMajiang = [];

        for (let mj of this.Content.children) mj.active = false;

        for (let i = 0; i < _name.length; i++) {
            this.Content.children[i].active = true;
            this.Content.children[i].name = _name[i];
            this.Content.children[i].getComponent(DiffSingleMajiang).UpdateMajiang(_name[i]);
            this.ShowMajiang.push(this.Content.children[i])
        }

        if (_isani) {
            if (_isani) {
                app.topManager.NoTouchClick()

                for (let mj of this.ShowMajiang) mj.active = false;

                for (let i = 0; i < this.ShowMajiang.length; i++) {
                    this.scheduleOnce(() => {
                        app.audioManager.PlayEffect(SOUND_TYPE.Boom);
                        this.ShowMajiang[i].active = true;
                    }, 0.1 * (this.ShowMajiang.length - i))

                }
                this.scheduleOnce(() => {
                    app.topManager.CanTouchClick();
                }, 0.1 * this.ShowMajiang.length)
            }
        }



    }

    /**
     * 点击解锁
     */
    private OnClickUnLock() {
        app.audioManager.PlayEffect(SOUND_TYPE.Button);
        app.adManager.ShowVideo(0, (_isend: boolean) => {
            if (_isend) {
                this.Content.active = true;
                this.UnLockBtn.active = false;

                this.Init([]);
                app.topManager.ShowPrompt("解锁成功");
            } else {
                app.topManager.ShowPrompt("复活失败");
            }
        })


    }

    /**
     * 锁住了
     */
    public Lock() {
        this.Content.active = false;
        this.UnLockBtn.active = true;
    }

    /**
     * 判断我是否可以被点击
     */
    public ReturnCanClick(): boolean {
        return this.Content.children[0].active;
    }

    /**
        * 恢复
        */
    public IamReset() {
        for (let mj of this.ClickMajiang) {
            mj.getComponent(DiffSingleMajiang).IamReset();
        }

        this.ClickMajiang = [];

        //更新显示麻将数组
        this.ShowMajiang = [];

        for (let majiang of this.Content.children) {
            if (majiang.active) {
                this.ShowMajiang.push(majiang);
            }
        }
    }

    //点击名称
    public ClickName: string = "";

    //显示麻将

    /**
     * 
     */
    public IamChoose() {
        //我最后一个麻将可以被点击的
        this.ClickName = this.ShowMajiang[this.ShowMajiang.length - 1].name;

        for (let i = this.ShowMajiang.length - 1; i >= 0; i--) {
            if (this.ShowMajiang[i].name == this.ClickName) {
                this.ClickMajiang.push(this.ShowMajiang[i]);
                this.ShowMajiang[i].getComponent(DiffSingleMajiang).IamChoose();
            } else {
                break;
            }
        };
    }

    /**
        * 返回最后的索引
        */
    public ReturnLastIndex(): string {
        if (this.ShowMajiang.length <= 0) return "";
        return this.ShowMajiang[this.ShowMajiang.length - 1].name;
    }

    public AddClickMajiang(_lmb: LittleMajiangBox) {

        //我缺少多少个
        let add_num: number = this.Content.children.length - this.ShowMajiang.length;
        let add_majiang: string = _lmb.ClickName;
        let add_majiangarr: Node[] = [];

        if (add_num > _lmb.ClickMajiang.length) add_num = _lmb.ClickMajiang.length;

        for (let i = 0; i < _lmb.ClickMajiang.length; i++) {
            if (add_num <= 0) break;
            add_majiangarr.push(_lmb.ClickMajiang[i])
            add_num--;
        }

        add_majiangarr = add_majiangarr.reverse();
        for (let i = 0; i < add_majiangarr.length; i++) {
            add_majiangarr[i].active = false;
            this.AddMajiang(add_majiangarr[i].getWorldPosition(), add_majiang)
        }

        _lmb.IamReset();
    }

    public IamWrong() {
        for (let majiang of this.ClickMajiang) {
            majiang.getComponent(DiffSingleMajiang).ImDown();
        }
        this.ClickMajiang = [];
    }

    public AddMajiang(_pos: Vec3, _name: string) {

        for (let majiang of this.Content.children) {
            if (!majiang.active) {
                majiang.active = true;
                majiang.getComponent(DiffSingleMajiang).UpdateMajiang(_name);
                majiang.getComponent(DiffSingleMajiang).PlayMoveAni(_pos);
                break;
            }
        }

        //更新显示麻将数组
        this.ShowMajiang = [];

        for (let majiang of this.Content.children) {
            if (majiang.active) {
                this.ShowMajiang.push(majiang);
            }
        }

        //如果我满了我判断是否可消除
        if (this.ShowMajiang.length == this.Content.children.length) {

            let clear_index: string = this.ShowMajiang[0].name;

            for (let i = 1; i < this.ShowMajiang.length; i++) {
                if (this.ShowMajiang[i].name != clear_index) {
                    // this.MajiangLoad.CheckGameOver();
                    return;
                }
            }

            //可消除
            app.topManager.NoTouchClick()
            app.audioManager.PlayEffect(SOUND_TYPE.Success);
            for (let i = this.ShowMajiang.length - 1; i >= 0; i--) {
                this.scheduleOnce(() => {
                    app.audioManager.PlayEffect(SOUND_TYPE.Boom);
                    this.ShowMajiang[i].active = false;
                    if (i == 0) {
                        this.ShowMajiang = [];
                        app.topManager.CanTouchClick();
                        //添加新的
                        this.DiffMajiang.AddMajiang(this);

                    }
                }, 0.1 * (this.ShowMajiang.length - i))
            }


        }
    }


}


